package com.hxd.otc.module.coin_deal.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.hxd.otc.R;
import com.hxd.otc.base.BaseAdapter;
import com.hxd.otc.module.coin_deal.bean.CoinDealResultBean;
import com.hxd.otc.utils.StringUtil;
import com.hxd.otc.utils.UIUtils;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by lzy on 2018/7/9 0009.
 * description ${我的挂单列表}
 */
public class MyEntryOrdersAdapter extends BaseAdapter<MyEntryOrdersAdapter.MyEntryOrdersHolder, CoinDealResultBean.DataBean> {

    private OnDeleteEntryOrdersListener onDeleteEntryOrdersListener;

    public MyEntryOrdersAdapter(Context context) {
        super(context);
    }

    @SuppressLint("DefaultLocale")
    @Override
    protected void onBindHolder(@NonNull MyEntryOrdersHolder holder, final int position) {
        CoinDealResultBean.DataBean item = getItem(position);

        if (!TextUtils.isEmpty(item.transPair)) {
            String[] splitstr = item.transPair.split("/");
            holder.tvStatues.setText(item.getSellStatus() == 0 ? UIUtils.getString(R.string.saleing, splitstr[0]) : UIUtils.getString(R.string.saled, splitstr[0]));
            holder.tvStatues.setTextColor(item.getSellStatus() == 0 ?UIUtils.getContext().getResources().getColor(R.color.color_FF5AC792):UIUtils.getContext().getResources().getColor(R.color.color_FFB4B4B4));
            holder.tvPriceUnit.setText(UIUtils.getString(R.string.unit_price, splitstr[1]));
            holder.tvNumberUnit.setText(UIUtils.getString(R.string.coint_count, splitstr[0]));
        }
        holder.tvNickname.setText(item.getNickName());
        holder.tvStatues.setVisibility(View.VISIBLE);
        holder.tvPrice.setText(StringUtil.format8Number(item.getPrice()));
        holder.tvAmount.setText(StringUtil.format4NumberWithoutZero(item.getRestAmount()));
        holder.tvDelete.setText(R.string.delete);
        holder.tvDelete.setBackgroundResource(R.drawable.selector_my_entry_order_btn_bg);
        holder.tvDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onDeleteEntryOrdersListener != null) {
                    onDeleteEntryOrdersListener.onDeleteEntryOrders(position);
                }
            }
        });
    }

    @NonNull
    @Override
    public MyEntryOrdersHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new MyEntryOrdersHolder(mInflater.inflate(R.layout.item_coin_view, parent, false));
    }

    static class MyEntryOrdersHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.name_tv)
        TextView tvNickname;
        @BindView(R.id.tv_statues)
        TextView tvStatues;
        @BindView(R.id.type_tv)
        TextView tvDelete;
        @BindView(R.id.tv_price)
        TextView tvPrice;
        @BindView(R.id.tv_unit_price)
        TextView tvPriceUnit;
        @BindView(R.id.hint_number_tv)
        TextView tvNumberUnit;
        @BindView(R.id.number_tv)
        TextView tvAmount;

        public MyEntryOrdersHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface OnDeleteEntryOrdersListener {
        void onDeleteEntryOrders(int position);
    }

    public void setOnDeleteEntryOrdersListener(OnDeleteEntryOrdersListener onDeleteEntryOrdersListener) {
        this.onDeleteEntryOrdersListener = onDeleteEntryOrdersListener;
    }
}
